package com.distribuida.supertrunfo.comum;

import java.io.Serializable;
import java.util.ArrayList;

@SuppressWarnings("serial")
public class Carta implements Comparable<Carta>, Serializable {
	
	private String codigo;
	private boolean trunfo;
	private boolean vencetrunfo;
	private ArrayList<Caracteristica> caracteristicas = new ArrayList<Caracteristica>(); 
	
	public Carta(String codigo, String trunfo, String vencetrunfo) {
		this.codigo = codigo;
		if(trunfo.equals("1"))
			this.trunfo = true;
		else
			this.trunfo = false;
		if(vencetrunfo.equals("1"))
			this.setVencetrunfo(true);
		else
			this.setVencetrunfo(false);
	}
	
	public String getCodigo() {
		return codigo;
	}
	public void setCodigo(String carta) {
		this.codigo = carta;
	}

	public boolean isTrunfo() {
		return trunfo;
	}
	public void setTrunfo(boolean trunfo) {
		this.trunfo = trunfo;
	}
	
	public boolean isVencetrunfo() {
		return vencetrunfo;
	}
	public void setVencetrunfo(boolean vencetrunfo) {
		this.vencetrunfo = vencetrunfo;
	}
	
	public ArrayList<Caracteristica> getCaracteristicas() {
		return caracteristicas;
	}
	public void setCaracteristicas(ArrayList<Caracteristica> caracteristicas) {
		this.caracteristicas = caracteristicas;
	}

	/**
	 * Seta o campo valor na caracteristica de contenha o id
	 * 
	 * @param id
	 * @param valor
	 */
	public void setValorCaracteristicas(String id, String valor) {
		for (Caracteristica caracteristica : caracteristicas) {
			if (caracteristica.getId().equals(id)) {
				if(caracteristica.getTipo().equals("nula")){
					caracteristica.setValorString(valor);
				}else{
					caracteristica.setValor(valor);
				}
			}
		}
	}
	
	@Override
	public int compareTo(Carta carta) {
		if (this.codigo.charAt(0) < carta.getCodigo().charAt(0))
			return -1;
		if (this.codigo.charAt(0) > carta.getCodigo().charAt(0))
			return 1;
		if (this.codigo.charAt(0) == carta.getCodigo().charAt(0)) {
			if (this.codigo.charAt(1) < carta.getCodigo().charAt(1))
				return -1;
			if (this.codigo.charAt(1) > carta.getCodigo().charAt(1))
				return 1;
		}
		return 0;
	}
	
	@Override
	public String toString(){
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("\nCodigo: ");
		stringBuffer.append(getCodigo());
		stringBuffer.append("\nSuper Trunfo: ");
		stringBuffer.append(isTrunfo());
		stringBuffer.append("\nVence Trunfo: ");
		stringBuffer.append(isVencetrunfo());
		stringBuffer.append("\nCaracteristicas: ");
		int n = 0;
		for(Caracteristica car : getCaracteristicas()){
			stringBuffer.append("\nID : ");
			stringBuffer.append(n + " - "  );
			n++;
			stringBuffer.append(car.getNome());
			if(car.getTipo().equals("nula")){
				stringBuffer.append(car.getValorString());	
			}else{
				stringBuffer.append(car.getValor());
			}
		}
		return stringBuffer.toString();
	}
}